﻿Date.prototype.format = function (format) {
    var o =
    {
        "M+": this.getMonth() + 1, //month   
        "d+": this.getDate(),    //day   
        "h+": this.getHours(),   //hour   
        "m+": this.getMinutes(), //minute   
        "s+": this.getSeconds(), //second   
        "q+": Math.floor((this.getMonth() + 3) / 3), //quarter  
        "S": this.getMilliseconds() //millisecond  
    }

    if (/(y+)/.test(format))
        format = format.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o)
        if (new RegExp("(" + k + ")").test(format))
            format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length));
    return format;
} 
Date.prototype.JSONDateToJSDate = function (jsondate) {
    var date = new Date(parseInt(jsondate.replace("/Date(", "").replace(")/", ""), 10));
    return date;
} 
String.prototype.toJSDate = function () {
    var date = new Date(parseInt(this.replace("/Date(", "").replace(")/", ""), 10));
    return date;
} 
Date.prototype.maxDayOfDate = function () {
    var myDate = this;
    var ary = myDate.toArray();
    var date1 = (new Date(ary[0], ary[1], 1));
    var date2 = date1.dateAdd('m', 1);
    var dd = date1.getMonth() + 1;
    var date2 = (new Date(ary[0], dd, 1));
    var result = parseInt((date2 - date1) / 86400000);
    return result;
}

Date.prototype.toArray = function (date2) {
    var myDate = date2;
    var myArray = Array();
    myArray[0] = myDate.getFullYear();
    myArray[1] = myDate.getMonth();
    myArray[2] = myDate.getDate();
    myArray[3] = myDate.getHours();
    myArray[4] = myDate.getMinutes();
    myArray[5] = myDate.getSeconds();
    return myArray;
}

Date.prototype.dateAdd = function (strInterval, Number) {
    var dtTmp = new Date();
    switch (strInterval) {
        case 's': return new Date(Date.parse(dtTmp) + (1000 * Number));
        case 'n': return new Date(Date.parse(dtTmp) + (60000 * Number));
        case 'h': return new Date(Date.parse(dtTmp) + (3600000 * Number));
        case 'd': return new Date(Date.parse(dtTmp) + (86400000 * Number));
        case 'w': return new Date(Date.parse(dtTmp) + ((86400000 * 7) * Number));
        case 'q': return new Date(dtTmp.getFullYear(), (dtTmp.getMonth()) + Number * 3, dtTmp.getDate(), dtTmp.getHours(), dtTmp.getMinutes(), dtTmp.getSeconds());
        case 'm': return new Date(dtTmp.getFullYear(), (dtTmp.getMonth()) + Number, dtTmp.getDate(), dtTmp.getHours(), dtTmp.getMinutes(), dtTmp.getSeconds());
        case 'y': return new Date((dtTmp.getFullYear() + Number), dtTmp.getMonth(), dtTmp.getDate(), dtTmp.getHours(), dtTmp.getMinutes(), dtTmp.getSeconds());
    }
}

Date.prototype.DateAdd2 = function (strInterval, Number) {
    switch (strInterval) {
        case 's': return new Date(Date.parse(this) + (1000 * Number));
        case 'n': return new Date(Date.parse(this) + (60000 * Number));
        case 'h': return new Date(Date.parse(this) + (3600000 * Number));
        case 'd': return new Date(Date.parse(this) + (86400000 * Number));
        case 'w': return new Date(Date.parse(this) + ((86400000 * 7) * Number));
        case 'q': return new Date(this.getFullYear(), (this.getMonth()) + Number * 3, this.getDate(), this.getHours(), this.getMinutes(), this.getSeconds());
        case 'm': return new Date(this.getFullYear(), (this.getMonth()) + Number, this.getDate(), this.getHours(), this.getMinutes(), this.getSeconds());
        case 'y': return new Date((this.getFullYear() + Number), this.getMonth(), this.getDate(), this.getHours(), this.getMinutes(), this.getSeconds());
    }
}

Date.prototype.dateDiff = function (strInterval, dtStart, dtEnd) {
    if (typeof dtEnd == 'string')//如果是字符串转换为日期型
    {
        dtEnd = StringToDate(dtEnd);
    }
    if (typeof dtStart == 'string')//如果是字符串转换为日期型
    {
        dtStart = StringToDate(dtStart);
    }
    switch (strInterval) {
        case 's': return parseInt((dtEnd - dtStart) / 1000);
        case 'n': return parseInt((dtEnd - dtStart) / 60000);
        case 'h': return parseInt((dtEnd - dtStart) / 3600000);
        case 'd': return parseInt((dtEnd - dtStart) / 86400000);
        case 'w': return parseInt((dtEnd - dtStart) / (86400000 * 7));
        case 'm': return (dtEnd.getMonth() + 1) + ((dtEnd.getFullYear() - dtStart.getFullYear()) * 12) - (dtStart.getMonth() + 1);
        case 'y': return dtEnd.getFullYear() - dtStart.getFullYear();
    }
} 
// 给日期类对象添加日期差方法，返回日期与diff参数日期的时间差，单位为天
Date.prototype.diff = function (date) {
    return (this.getTime() - date.getTime()) / (24 * 60 * 60 * 1000);
}
Array.prototype.contains = function (item) {
    var ret = false;
    for (var i = 0; i < this.length; i++) {
        if (this[i] == item) {
            ret = true;
            break;
        }
    }
    return ret;
};
Array.prototype.indexOf = function (val) {
    for (var i = 0; i < this.length; i++) {
        if (this[i] == val) return i;
    }
    return -1;
}; 
Array.prototype.remove = function (val) {
    var index = this.indexOf(val);
    if (index > -1) {
        this.splice(index, 1);
    }
}; 
$(document).ajaxError(function (e, xhr, settings, thrownError) {
    console.log("执行[" + settings.url + "]时出错，错误类型:" + xhr.status + "(" + xhr.statusText + ")");
    return false;
});
var Yz = (function (module) {
    //判断是否为空
    module.IsNullOrEmpty = function (str) {
        var suc = (str == undefined || str == null || str == "");
        return suc;
    };
    //判断是否为空或为0
    module.IsNullOrZero = function (str) {
        var suc = (str == undefined || str == null || str == "" || str == 0);
        return suc;
    };
    module.validKeyWord = function (value) {
        return value.replace(/[^\a-\z\A-\Z0-9\u4E00-\u9FA5\@\.]/g, '');
    }
    ///下载
    module.addForm = function (actionName, arrayData) {
        var html = "<div id='divForm'  ><form name='submitForm' id='submitForm' method='post' action='" + actionName + "'>";
        for (var item in arrayData) {
            html += "<input id='" + item + "' name='" + item + "' value='" + arrayData[item] + "'/>";
        }
        html += "<input id='btnSubmit'  type='submit'>";
        html += "</form></div>";
        $("body").append(html);
        $("#btnSubmit").click();
        $("#divForm").html();
        $("#divForm").remove();
    }
    ///对象深拷贝
    module.objDeepCopy = function (source) {
        var sourceCopy = {};
        for (var item in source) {
            sourceCopy[item] = typeof source[item] === 'object' ? objDeepCopy(source[item]) : source[item];
        }
        return sourceCopy;
    }
    /*1.用浏览器内部转换器实现html转码*/
    module.htmlEncode = function (html) {
        //1.首先动态创建一个容器标签元素，如DIV
        var temp = document.createElement("div");
        //2.然后将要转换的字符串设置为这个元素的innerText(ie支持)或者textContent(火狐，google支持)
        (temp.textContent != undefined) ? (temp.textContent = html) : (temp.innerText = html);
        //3.最后返回这个元素的innerHTML，即得到经过HTML编码转换的字符串了
        var output = temp.innerHTML;
        temp = null;
        return output;
    }
    /*2.用浏览器内部转换器实现html解码*/
    module.htmlDecode = function (text) {
        //1.首先动态创建一个容器标签元素，如DIV
        var temp = document.createElement("div");
        //2.然后将要转换的字符串设置为这个元素的innerHTML(ie，火狐，google都支持)
        temp.innerHTML = text;
        //3.最后返回这个元素的innerText(ie支持)或者textContent(火狐，google支持)，即得到经过HTML解码的字符串了。
        var output = temp.innerText || temp.textContent;
        temp = null;
        return output;
    }
    module.keyBoardClick = function (enterClick, escClick) {
        document.onkeydown = function (event) {
            var e = event || window.event || arguments.callee.caller.arguments[0];
            if (e && e.keyCode == 13) { // enter 键
                enterClick();
            } else if (e && e.keyCode == 113) { // 按 F2 
                if (escClick != undenfined && escClick != null) {
                    escClick();
                }
            }
        };
    }
    module.GUID = function () {
        this.date = new Date();   /* 判断是否初始化过，如果初始化过以下代码，则以下代码将不再执行，实际中只执行一次 */
        if (typeof this.newGUID != 'function') {   /* 生成GUID码 */
            GUID.prototype.newGUID = function () {
                this.date = new Date(); var guidStr = '';
                sexadecimalDate = this.hexadecimal(this.getGUIDDate(), 16);
                sexadecimalTime = this.hexadecimal(this.getGUIDTime(), 16);
                for (var i = 0; i < 9; i++) {
                    guidStr += Math.floor(Math.random() * 16).toString(16);
                }
                guidStr += sexadecimalDate;
                guidStr += sexadecimalTime;
                while (guidStr.length < 32) {
                    guidStr += Math.floor(Math.random() * 16).toString(16);
                }
                return this.formatGUID(guidStr);
            }
            /* * 功能：获取当前日期的GUID格式，即8位数的日期：19700101 * 返回值：返回GUID日期格式的字条串 */
            GUID.prototype.getGUIDDate = function () {
                return this.date.getFullYear() + this.addZero(this.date.getMonth() + 1) + this.addZero(this.date.getDay());
            }
            /* * 功能：获取当前时间的GUID格式，即8位数的时间，包括毫秒，毫秒为2位数：12300933 * 返回值：返回GUID日期格式的字条串 */
            GUID.prototype.getGUIDTime = function () {
                return this.addZero(this.date.getHours()) + this.addZero(this.date.getMinutes()) + this.addZero(this.date.getSeconds()) + this.addZero(parseInt(this.date.getMilliseconds() / 10));
            }
            /* * 功能: 为一位数的正整数前面添加0，如果是可以转成非NaN数字的字符串也可以实现 * 参数: 参数表示准备再前面添加0的数字或可以转换成数字的字符串 * 返回值: 如果符合条件，返回添加0后的字条串类型，否则返回自身的字符串 */
            GUID.prototype.addZero = function (num) {
                if (Number(num).toString() != 'NaN' && num >= 0 && num < 10) {
                    return '0' + Math.floor(num);
                } else {
                    return num.toString();
                }
            }
        /*  * 功能：将y进制的数值，转换为x进制的数值 * 参数：第1个参数表示欲转换的数值；第2个参数表示欲转换的进制；第3个参数可选，表示当前的进制数，如不写则为10 * 返回值：返回转换后的字符串 */GUID.prototype.hexadecimal = function (num, x, y) {
                if (y != undefined) { return parseInt(num.toString(), y).toString(x); }
                else { return parseInt(num.toString()).toString(x); }
            }
            /* * 功能：格式化32位的字符串为GUID模式的字符串 * 参数：第1个参数表示32位的字符串 * 返回值：标准GUID格式的字符串 */
            GUID.prototype.formatGUID = function (guidStr) {
                var str1 = guidStr.slice(0, 8) + '-', str2 = guidStr.slice(8, 12) + '-', str3 = guidStr.slice(12, 16) + '-', str4 = guidStr.slice(16, 20) + '-', str5 = guidStr.slice(20);
                return str1 + str2 + str3 + str4 + str5;
            }
        }
    }
    window.Yz = module;
    return module;
})(Yz || {})


